package com.lottery.material.controller.admin.article;

import com.lottery.material.common.EnumInfo;
import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.enumeration.article.ArticleInfoEnum;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.pojo.article.ArticleInfo;
import com.lottery.material.pojo.circle.CircleInfo;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.pojocustom.article.ArticleInfoCustom;
import com.lottery.material.service.admin.article.ArticleInfoService;
import com.lottery.material.service.admin.circle.CircleInfoService;
import com.lottery.material.util.EnumUtil;
import com.lottery.material.util.Jurisdiction;
import com.lottery.material.util.PdUtil;
import com.lottery.material.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description：文章信息Controller
 * Created by 朱正磊 on 2019-04-13 20:15
 */
@Controller("adminArticleInfoController")
@RequestMapping("/admin/articleInfo")
public class ArticleInfoController extends BaseController {

    //菜单地址，用于权限控制
    private String menuUrl = "admin/articleInfo/list";

    private final ArticleInfoService articleInfoService;

    private final CircleInfoService circleInfoService;

    @Autowired
    public ArticleInfoController(ArticleInfoService articleInfoService,
                                 CircleInfoService circleInfoService) {
        this.articleInfoService = articleInfoService;
        this.circleInfoService = circleInfoService;
    }

    /**
     * description：分页列表
     * user 朱正磊
     * time 2019-04-17 15:06
     * @param page 分页对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/list")
    public ModelAndView listPage(Page page, Map<String, Object> map) {
        //分页对象设置页面请求参数
        PageData pd = getPageData();
        PdUtil.rePut(pd);
        pd.put("isDelete", CommonEnum.IS_DELETE_FALSE.getValue());//查询未逻辑删除的数据
        page.setPd(pd);

        //查询分页列表
        List<ArticleInfoCustom> list = articleInfoService.findCustomListPage(page);
        map.put("list", list);
        map.put("pd", pd);
        map.put("QX", Jurisdiction.getHC());

        //审核状态
        List<EnumInfo> articleStateList = EnumUtil.getEnumInfoList(ArticleInfoEnum.ARTICLE_STATE, ArticleInfoEnum.class);
        map.put("articleStateList", articleStateList);

        //所属圈子（所有的圈子，包括下架的圈子）
        List<CircleInfo> circleInfoList = circleInfoService.findListByState(null);
        map.put("circleInfoList", circleInfoList);

        //是否收费
        List<EnumInfo> isChargeList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_CHARGE, ArticleInfoEnum.class);
        map.put("isChargeList", isChargeList);

        //收费文章是否公开（1：不公开；2：公开）
        List<EnumInfo> isEndPublicList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_END_PUBLIC, ArticleInfoEnum.class);
        map.put("isEndPublicList", isEndPublicList);

        //收费文章公开状态（1：未公开；2：已公开）
        List<EnumInfo> publicStateList = EnumUtil.getEnumInfoList(ArticleInfoEnum.PUBLIC_STATE, ArticleInfoEnum.class);
        map.put("publicStateList", publicStateList);

        //是否进热贴（1：否；2：是）
        List<EnumInfo> isHotList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_HOT, ArticleInfoEnum.class);
        map.put("isHotList", isHotList);

        return new ModelAndView("article/articleInfo/listPage", map);
    }

    /**
     * description：详情页面
     * user 朱正磊
     * time 2019-04-17 16:20
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/detailPage")
    public ModelAndView detailPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")) {//判断是否有查看权限
            return null;
        }

        //根据主键查询扩展类
        ArticleInfoCustom articleInfo = articleInfoService.findCustomById(id);
        map.put("articleInfo", articleInfo);

        //所有的圈子列表
        List<CircleInfo> circleInfoList = circleInfoService.findListByState(null);
        map.put("circleInfoList", circleInfoList);

        //是否推广（1：否；2：是）
        List<EnumInfo> isPromotionList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_PROMOTION, ArticleInfoEnum.class);
        map.put("isPromotionList", isPromotionList);

        //是否进热贴（1：否；2：是）
        List<EnumInfo> isHotList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_HOT, ArticleInfoEnum.class);
        map.put("isHotList", isHotList);

        //是否置顶（1：否；2：是）
        List<EnumInfo> isTopList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_TOP, ArticleInfoEnum.class);
        map.put("isTopList", isTopList);

        //是否收费
        List<EnumInfo> isChargeList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_CHARGE, ArticleInfoEnum.class);
        map.put("isChargeList", isChargeList);

        //审核状态
        List<EnumInfo> articleStateList = EnumUtil.getEnumInfoList(ArticleInfoEnum.ARTICLE_STATE, ArticleInfoEnum.class);
        articleStateList.remove(0);
        map.put("articleStateList", articleStateList);

        return new ModelAndView("article/articleInfo/detailPage", map);
    }

    /**
     * description：添加页面
     * user 朱正磊
     * time 2019-04-17 16:26
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/addPage")
    public ModelAndView addPage(Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {//判断是否有添加权限
            return null;
        }

        //上架的圈子列表
        List<CircleInfo> circleInfoList = circleInfoService.findListByState((Integer) CommonEnum.STATE_ON.getValue());
        map.put("circleInfoList", circleInfoList);

        //审核状态（新增其实不需要该枚举，后台新增默认设置为待审核）
        List<EnumInfo> articleStateList = EnumUtil.getEnumInfoList(ArticleInfoEnum.ARTICLE_STATE, ArticleInfoEnum.class);
        map.put("articleStateList", articleStateList);

        //添加请求地址
        map.put("actionUrl", "admin/articleInfo/add");

        return new ModelAndView("article/articleInfo/addPage", map);
    }

    /**
     * description：添加
     * user 朱正磊
     * time 2019-04-17 16:39
     * @param articleInfo 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/add")
    public ModelAndView add(ArticleInfo articleInfo, Map<String, Object> map) {
        Integer flag = articleInfoService.add(articleInfo);
        if (Objects.equals(flag, 0)) { //添加失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：审核页面
     * user 朱正磊
     * time 2019-04-17 16:48
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/auditPage")
    public ModelAndView auditPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }

        //根据主键查询扩展类
        ArticleInfoCustom articleInfo = articleInfoService.findCustomById(id);
        map.put("articleInfo", articleInfo);

        //所有的圈子列表
        List<CircleInfo> circleInfoList = circleInfoService.findListByState(null);
        map.put("circleInfoList", circleInfoList);

        //是否推广（1：否；2：是）
        List<EnumInfo> isPromotionList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_PROMOTION, ArticleInfoEnum.class);
        map.put("isPromotionList", isPromotionList);

        //是否进热贴（1：否；2：是）
        List<EnumInfo> isHotList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_HOT, ArticleInfoEnum.class);
        map.put("isHotList", isHotList);

        //是否置顶（1：否；2：是）
        List<EnumInfo> isTopList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_TOP, ArticleInfoEnum.class);
        map.put("isTopList", isTopList);

        //是否收费
        List<EnumInfo> isChargeList = EnumUtil.getEnumInfoList(ArticleInfoEnum.IS_CHARGE, ArticleInfoEnum.class);
        map.put("isChargeList", isChargeList);

        //审核状态
        List<EnumInfo> articleStateList = EnumUtil.getEnumInfoList(ArticleInfoEnum.ARTICLE_STATE, ArticleInfoEnum.class);
        articleStateList.remove(0);
        map.put("articleStateList", articleStateList);

        //修改请求地址
        map.put("actionUrl", "admin/articleInfo/audit");

        return new ModelAndView("article/articleInfo/auditPage", map);
    }

    /**
     * description：文章审核
     * user 朱正磊
     * time 2019-04-17 16:58
     * @param request 请求对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/audit")
    public ModelAndView audit(HttpServletRequest request, Map<String, Object> map) {
        Integer flag = articleInfoService.updateByAudit(request);
        if (Objects.equals(flag, 0)) { //修改失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：修改阅读量页面
     * user 朱正磊
     * time 2019-06-03 18:06
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/editReadTotalPage")
    public ModelAndView editReadTotalPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }

        //根据主键查询扩展类
        ArticleInfoCustom articleInfo = articleInfoService.findCustomById(id);
        map.put("articleInfo", articleInfo);

        //修改阅读量请求地址
        map.put("actionUrl", "admin/articleInfo/editReadTotal");

        return new ModelAndView("article/articleInfo/editReadTotalPage", map);
    }

    /**
     * description：修改阅读量
     * user 朱正磊
     * time 2019-04-17 16:58
     * @param articleInfo 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/editReadTotal")
    public ModelAndView editReadTotal(ArticleInfo articleInfo, Map<String, Object> map) {
        Integer flag = articleInfoService.updateById(articleInfo);
        if (Objects.equals(flag, 0)) { //修改失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：根据主键删除一条数据（逻辑删除）
     * user 朱正磊
     * time 2019-04-30 11:26
     * @param id 主键
     * @return 返回响应对象
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(Integer id) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")) {//判断是否有删除权限
            return null;
        }
        Integer flag = articleInfoService.deleteLogicById(id);
        if (Objects.equals(0, flag)) {
            return ResponseResult.buildResult(400, "删除失败！");
        } else {
            return ResponseResult.buildResult(200, "删除成功！");
        }
    }

}
